<?php declare(strict_types=1);

/**
 * @package ExtendedMetaTags (Light Portal)
 * @link https://custom.simplemachines.org/index.php?mod=4244
 * @author Bugo <bugo@dragomano.ru>
 * @copyright 2021-2025 Bugo
 * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later
 *
 * @category plugin
 * @version 17.10.25
 */

namespace LightPortal\Plugins\ExtendedMetaTags;

use Bugo\Compat\Utils;
use LightPortal\Enums\ForumHook;
use LightPortal\Enums\PluginType;
use LightPortal\Enums\Tab;
use LightPortal\Plugins\Event;
use LightPortal\Plugins\Plugin;
use LightPortal\Plugins\PluginAttribute;
use LightPortal\UI\Fields\VirtualSelectField;

if (! defined('LP_NAME'))
	die('No direct access...');

/**
 * Generated by PluginMaker
 */
#[PluginAttribute(type: [PluginType::PAGE_OPTIONS, PluginType::SEO])]
class ExtendedMetaTags extends Plugin
{
	private array $meta_robots = ['', 'index, follow', 'index, nofollow', 'noindex, follow', 'noindex, nofollow'];

	private array $meta_rating = ['', '14 years', 'adult', 'general', 'mature', 'restricted', 'save for kids'];

	public function init(): void
	{
		$this->applyHook(ForumHook::themeContext);
	}

	public function themeContext(): void
	{
		if ($this->request()->hasNot('page') || empty(Utils::$context['lp_page']['options']))
			return;

		if (! empty(Utils::$context['lp_page']['options']['meta_robots'])) {
			Utils::$context['meta_tags'][] = [
				'name' => 'robots', 'content' => Utils::$context['lp_page']['options']['meta_robots']
			];
		}

		if (! empty(Utils::$context['lp_page']['options']['meta_rating'])) {
			Utils::$context['meta_tags'][] = [
				'name' => 'rating', 'content' => Utils::$context['lp_page']['options']['meta_rating']
			];
		}
	}

	public function preparePageParams(Event $e): void
	{
		$e->args->params['meta_robots'] = '';
		$e->args->params['meta_rating'] = '';
	}

	public function validatePageParams(Event $e): void
	{
		$e->args->params['meta_robots'] = FILTER_DEFAULT;
		$e->args->params['meta_rating'] = FILTER_DEFAULT;
	}

	public function preparePageFields(Event $e): void
	{
		VirtualSelectField::make('meta_robots', $this->txt['meta_robots'])
			->setTab(Tab::SEO)
			->setOptions(array_combine($this->meta_robots, $this->txt['meta_robots_set']))
			->setValue($e->args->options['meta_robots']);

		VirtualSelectField::make('meta_rating', $this->txt['meta_rating'])
			->setTab(Tab::SEO)
			->setOptions(array_combine($this->meta_rating, $this->txt['meta_rating_set']))
			->setValue($e->args->options['meta_rating']);
	}
}
